package com.douqu.game.main.test;


import com.douqu.game.core.protobuf.*;
import com.douqu.game.core.util.BufferUtils;
import com.douqu.game.core.util.SendUtils;
import com.douqu.game.core.util.CoreUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import com.douqu.game.core.util.LogUtils;


/**
* Created by Administrator on 2016/11/9.
*/
public class NettyTCPClientHandler extends ChannelInboundHandlerAdapter {


    public String battleId;

    public String playerIndex;

    public String account;

    public NettyTCPClientHandler(String account) {
        this.account = account;
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
    {
//        cause.printStackTrace();
        LogUtils.info(cause.getLocalizedMessage());
        ctx.close();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf) msg;
        int code = BufferUtils.readShort(byteBuf);

        LogUtils.info("code: " + code + " data length: " + byteBuf.readableBytes());


        byte[] data = CoreUtils.byteBufToBytes(byteBuf);
        switch (code){
            case SGMainProto.E_MSG_ID.MsgID_System_NotifyAlert_VALUE:
                SGSystemProto.S2C_NotifyAlert error = SGSystemProto.S2C_NotifyAlert.parseFrom(data);
                LogUtils.info("收到错误消息:" + error.getType());
                break;
            case SGMainProto.E_MSG_ID.MsgID_System_Login_VALUE:
//                LogUtils.info(SGSystemProto.S2C_Login.parseFrom(data).toString());
                LogUtils.info("收到登录返回消息!，发送任务列表消息");
//                Thread.sleep(500);
                SGTaskProto.C2S_TaskList.Builder req = SGTaskProto.C2S_TaskList.newBuilder();
                req.setType(SGCommonProto.E_TASK_TYPE.TASK_TYPE_MAIN_LINE);
                SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_Task_TaskList_VALUE, req.build().toByteArray());
                break;
            case SGMainProto.E_MSG_ID.MsgID_Task_TaskList_VALUE:
                SGTaskProto.S2C_TaskList info = SGTaskProto.S2C_TaskList.parseFrom(data);
                LogUtils.info("收到任务列表结果:" + info);
//                battleId = info.getBattleId();
//                SGCommonProto.ServerInfo serverInfo = info.getServerInfo();
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
////                        new TestTCP().start(serverInfo.getHost(), serverInfo.getPort(), playerIndex, battleId);
//                    }
//                }).start();
                break;
            case SGMainProto.E_MSG_ID.MsgID_System_Regist_VALUE:
                LogUtils.info(SGSystemProto.S2C_Regist.parseFrom(data).toString());
                break;
        }


    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();// 将消息发送队列中的消息写入到SocketChannel中发送给对方。
    }


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception
    {

//        register(ctx);
//        login(ctx);
//        register(ctx);
//
//        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_System_SuperLogin_VALUE, null);
//
//        ctx.close();
        login(ctx);
        //SendUtils.sendMsg(ctx.channel(), 1);
    }

    private void login(ChannelHandlerContext ctx) {
        SGSystemProto.C2S_Login.Builder request = SGSystemProto.C2S_Login.newBuilder();
        request.setChannel(SGCommonProto.E_CHANNEL_TYPE.CHANNEL_TYPE_QUICK);
        request.setNormal(true);
        request.setLoginType(SGCommonProto.E_LOGIN_TYPE.LOGIN_TYPE_DEFAULT);
        request.setServerRuleId(286);
        request.setPassword("565507085C83BAC4C79A0014B05D7A4C");
        request.setClientVersion("1.1");
        request.setAccount(account);

        System.out.println("登录账号　 -> " + account);

        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_System_Login_VALUE, request.build().toByteArray());
    }


    private void register(ChannelHandlerContext ctx) {
        SGSystemProto.C2S_Regist.Builder request = SGSystemProto.C2S_Regist.newBuilder();
        request.setChannel(SGCommonProto.E_CHANNEL_TYPE.CHANNEL_TYPE_QUICK);
        request.setAccount("quickregister");
        request.setAvatarUrl("fff.jpg");
        request.setNickName("zailaiyigeasdsad");

        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_System_Regist_VALUE, request.build().toByteArray());
    }

    private void recharge(ChannelHandlerContext ctx){
        SGPlayerProto.C2S_RechargeCheck.Builder request = SGPlayerProto.C2S_RechargeCheck.newBuilder();
        request.setRecharId(3);
        request.setRechargeType(SGCommonProto.E_RECHARGE_TYPE.RECHARGE_TYPE_YJ);
        request.setParam("nihao");
        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_Bonus_RechargeCheck_VALUE,request.build().toByteArray());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception
    {
        LogUtils.info("服务器已关闭，我也要关服了！");
        ctx.close();
        System.exit(0);
    }


}